package com.zhu.service.impl;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhu.mapper.LoginlogMapper;
import com.zhu.pojo.Loginlog;
import com.zhu.query.LoginLogQuery;
import com.zhu.service.ILoginlogService;
import com.zhu.utils.AddressUtils;
import com.zhu.utils.IpUtil;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xiaozhu
 * @since 2022-09-30
 */
@Service
public class LoginlogServiceImpl extends ServiceImpl<LoginlogMapper, Loginlog> implements ILoginlogService {

    @Override
    public void loginLog(HttpServletRequest request, Integer status, String msg, String username) {
        Loginlog loginlog = new Loginlog();
        //将用户的定位以及ip地址进行赋值
        Map location = AddressUtils.getAddress(IpUtil.getIp(request));
        //将用户的访问信息进行赋值
        IpUtil.setBrowser(request, loginlog);
        loginlog.setIp((String) location.get("ip"));
        loginlog.setLocation(location.get("pro").toString() + location.get("city"));
        loginlog.setStatus(status);
        loginlog.setMessage(msg);
        loginlog.setUsername(username);
        loginlog.setLoginTime(new Date());
        save(loginlog);
    }

    @Override
    public List<Loginlog> getLog(LoginLogQuery loginLogQuery) {
        return baseMapper.getLog(loginLogQuery);
    }

}
